const app = getApp()
Page({
  data: {
    stuKtmax: '',
    teaStumax: ''
  },
  onLoad: function(options) {
    const that = this
    wx.cloud.database({
      env: 'gtss-2b1812'
    }).collection('Config').where({
      dept: app.globalData.userDept
    }).get().then(res => {
      that.setData({
        stuKtmax: res.data[0].stuKtmax,
        teaStumax: res.data[0].teaStumax
      })
    })

  },
  inpTop: function(e) {
    this.setData({
      stuKtmax: e.detail.value
    })
  },
  inpStu: function(e) {
    this.setData({
      teaStumax: e.detail.value
    })
  },
  saveSetup: function() {
    const that = this
    if (that.data.stuKtmax == '') {
      wx.showToast({
        title: '学生最多同时申请课题数不能为空',
        icon: 'none',
        duration: 2000
      })
    } else if (that.data.teaStumax == '') {
      wx.showToast({
        title: '导师最多可指导学生数不能为空',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.cloud.callFunction({
        name: 'choiceTopicSetup',
        data: {
          stuKtmax: parseInt(that.data.stuKtmax),
          teaStumax: parseInt(that.data.teaStumax),
          dept: app.globalData.userDept
        }
      }).then(res => {
        if (res.result.result == 1) {
          wx.showToast({
            title: '保存成功！',
            icon: 'success',
            duration: 2000
          })
        } else if (res.result.result == 0) {
          wx.showToast({
            title: '保存失败！',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  }
})